package com.huitone.gddw.service.impl.worktable;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.BlackUse;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IBlackUseDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IBlackUseService;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.dto.BlackUseDTO;
import com.huitone.gddw.utils.StringUtils;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 黑名单用户 接口实现类
 * @author wuluodan
 *
 */
@Service
public class BlackUseService implements IBlackUseService {

	@Autowired
	private IBlackUseDao blackUseDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<BlackUseDTO> selectPage(QueryPage<BlackUse> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("svrNode", page.getParams().get("svrNode"));
		map.put("serviceCode", page.getParams().get("serviceCode"));
		map.put("blackUser", page.getParams().get("blackUser"));
		map.put("status", page.getParams().get("status"));
		map.put("source", page.getParams().get("source"));
		map.put("startDate", page.getParams().get("startDate"));
		map.put("endDate", page.getParams().get("endDate"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<BlackUseDTO> result = blackUseDao.selectPage(map);
		int totalCount = blackUseDao.selectCount(map);
		
		QueryPage<BlackUseDTO> queryPage = new QueryPage<BlackUseDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public BlackUse selectByPrimaryKey(String recId) {
		return blackUseDao.selectByPrimaryKey(recId);
	}

	@Override
	public BlackUseDTO toUpdate(String recId) {
		return new BlackUseDTO(this.selectByPrimaryKey(recId));
	}

	@Override
	public void changeStatus(BlackUse entity) {
		this.update(entity);
	}

	@Override
	public void save(BlackUse entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		blackUseDao.insertSelective(entity);
		
		this.updateParamRec("保存黑名单用户");
	}

	@Override
	public void update(BlackUse entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		blackUseDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新黑名单用户");
	}

	@Override
	public void delete(String recId) {
		blackUseDao.deleteByPrimaryKey(recId);
		this.updateParamRec("删除黑名单用户");
	}

	@Override
	public List<BlackUseDTO> getRecord(Map<String, Object> params) {
		List<BlackUseDTO> list = blackUseDao.getRecord(params);
		return list;
	}

	@Override
	public int saveBatch(List<BlackUse> list) {
		for (BlackUse blackUse : list) {
			blackUse.setRecId(StringUtils.uuid());
			blackUse.setInsertTime(new Date());
			blackUse.setInsertStaffId(SysLoginUtil.getLoginUserId());
			blackUse.setLastModTime(blackUse.getInsertTime());
			blackUse.setLastModStaffId(blackUse.getInsertStaffId());
		}
		if (!list.isEmpty() && list.size() > 0) {
			this.updateParamRec("批量保存黑名单用户");
			return blackUseDao.insertbatch(list);
		}
		return 0;
	}

	@Override
	public void delAllByFileIds(List<String> list) {
		if (!list.isEmpty() && list.size() > 0) {
			blackUseDao.delAllByFileIds(list);
		}
		this.updateParamRec("根据多个文件ID删除黑名单用户");
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.BLACKUSE, desc);
	}

	@Override
	public void changeStatusByFileIds(String[] fileIds, String status) {
		List<String> list = Arrays.asList(fileIds);
		if (!list.isEmpty() && list.size() > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("status", status);
			map.put("list", list);
			blackUseDao.changeStatus(map);
		}
	}

}
